
package com.kingoin.kens.sys.core.mybatis.fieldfill;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.ReflectionException;
import org.gg.boom.common.utils.SecurityUtils;

import java.time.LocalDateTime;

@Slf4j
public class CustomMetaObjectHandler implements MetaObjectHandler {
    private static final String CREATE_USER = "createUser";
    private static final String CREATE_TIME = "createTime";
    private static final String CREATE_USERNAME = "createUsername";
    private static final String UPDATE_USER = "updateUser";
    private static final String UPDATE_TIME = "updateTime";
    private static final String UPDATE_USERNAME = "updateUsername";
    private static final String DEL_FLAG = "delFlag";


    public void insertFill(MetaObject metaObject) {
        try {
            this.setFieldValByName("createBy", this.getUsername(), metaObject);
            this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        } catch (ReflectionException var3) {
            log.warn(">>> CustomMetaObjectHandler处理过程中无相关字段，不做处理");
        }

    }

    public void updateFill(MetaObject metaObject) {
        try {
            this.setFieldValByName("updateBy", this.getUsername(), metaObject);
            this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        } catch (ReflectionException var3) {
            log.warn(">>> CustomMetaObjectHandler处理过程中无相关字段，不做处理");
        }

    }

    private Long getUserUniqueId() {
        try {
            return SecurityUtils.getUserId();
        } catch (Exception var2) {
            return -1L;
        }
    }

    private String getUsername() {
        try {
            return SecurityUtils.getUsername();
        } catch (Exception var2) {
            return "";
        }
    }
}
